package _struct

import (
	"bytes"
	"fmt"
	"io"
	"os"
)

type Sayer interface {
	say()
}

type Mover interface {
	move()
}

//接口多重继承
type Animal2 interface {
	Sayer
	Mover
}

type Dog2 struct {
	name string
}

type Cat2 struct {
	name string
}

func (d Dog2) say() { //实现了值传递，自动实现了指针传递
	fmt.Println("汪汪汪")
}

func (c Cat2) say() {
	fmt.Println("喵喵喵")
}

func (d *Dog2) move() { //实现了指针传递，不会实现值传递
	fmt.Println("购汇东")
}

func (c *Cat2) move() {
	fmt.Println("毛会东")
}

func TestInterface() {
	var cat = Cat2{"猫"}
	var dog = Dog2{"狗"}

	var sayers = []Sayer{cat, dog}
	for _, v := range sayers {
		v.say()
		fmt.Println(v)
	}

	var sayers2 = []Sayer{&cat, &dog}
	for _, v := range sayers2 {
		v.say()
		fmt.Println(v)
	}

	var movers = []Mover{&cat, &dog} //只实现了指针传递，因此此处传指针
	for _, v := range movers {
		v.move()
		fmt.Println(v)
	}

	var x Mover
	x = &Dog2{"dd"}
	fmt.Println(x)
	x.move()

	p := &Dog2{}
	x = p
	x.move()

	var d = Dog2{}
	d.say()
	d.move()
	x = &d

	var peo people = &student2{}
	s := peo.speak("haha")
	fmt.Println(s)
}

type people interface {
	speak(string) string
}

type student2 struct {
}

func (stu *student2) speak(think string) (talk string) {
	if think == "sb" {
		talk = "你是个大帅比"
	} else {
		talk = "您好"
	}
	return
}

func TestInterface2() {
	var a interface{}
	a = 1
	fmt.Println(a)

	a = "aa"
	fmt.Println(a)

	a = false
	fmt.Println(a)

	var stu = make(map[string]interface{})
	stu["name"] = "aa"
	stu["age"] = 18
	stu["married"] = false

	fmt.Println(stu)

	var w io.Writer
	w = os.Stdout
	fmt.Println(w)
	w = new(bytes.Buffer)
	fmt.Println(w)
	w = nil
	fmt.Println(w)

	var x interface{}
	x = "aa"
	v, ok := x.(string)
	if ok {
		fmt.Println(v)
	}

	judgeType("aa")
	judgeType(1)
	judgeType(false)
	judgeType(2.3)

}

func judgeType(x interface{}) {
	switch t := x.(type) {
	case string:
		fmt.Println("string")
	case int:
		fmt.Println("int")
	case bool:
		fmt.Println("bool")
	default:
		fmt.Println("other")
		fmt.Println(t)
	}
}
